const { GuanceSourceMapUploadWebpackPlugin } = require('@cloudcare/webpack-plugin')

export default {
  server: {
    host: '0.0.0.0',
    port: 3000
  },
  // 暴露环境变量便于插件引用
  publicRuntimeConfig: {
    cloudcare: {
      applicationId: process.env.CLOUDCARE_APPLICATION_ID,
      site: process.env.CLOUDCARE_SITE,
      clientToken: process.env.CLOUDCARE_CLIENT_TOKEN,
      env: process.env.CLOUDCARE_ENV,
      version: process.env.CLOUDCARE_VERSION,
      service: process.env.CLOUDCARE_SERVICE
    }
  },
  // 仅为 Client 运行时引入 RUM SDK
  plugins: [
    { src: '@/plugins/cloudcare', mode: 'client' }
  ],
  // 仅为 Client 非开发环境构建时引入 Sourcemap 上传插件
  build: {
    extend(config, { isClient, isDev }) {
      if (isClient) {
        // 生成 Sourcemap
        config.devtool = 'source-map';
        //devtool: 'hidden-source-map',
        // 在非开发环境构建时引入插件，否则执行 `npm run dev` 时，Client 构建的进度会停留在 98%，导致应用无法访问
        if (!isDev) {
          config.plugins.push(
            new GuanceSourceMapUploadWebpackPlugin({
              applicationId: process.env.CLOUDCARE_APPLICATION_ID,
              apiKey: process.env.CLOUDCARE_API_KEY,
              server: process.env.CLOUDCARE_SERVER,
              filepaths: ['.nuxt/dist/client'],
              logLevel: 'verbose',
              root: '_nuxt',
              env: process.env.CLOUDCARE_ENV,
              version: process.env.CLOUDCARE_VERSION
            })
          )
        }
      }
    }
  }
}
